package threadcoreknowledge.createthread.sixstates;

/**
 * 线程的后三种情况Blocked , Waiting , TIMED_WAITING
 *
 * @Author Li Cheng
 * @Date 2021/8/11 15:25
 * @Version 1.0
 */
public class BlockedWaitingTimedWaiting implements Runnable {

    public static void main(String[] args) throws InterruptedException {
        BlockedWaitingTimedWaiting runnable = new BlockedWaitingTimedWaiting();

        Thread thread1 = new Thread(runnable);
        thread1.start();

        Thread.sleep(1000);

        Thread thread2 = new Thread(runnable);
        thread2.start();


        //打印出timed_waiting因为正在执行sleep
        System.out.println(thread1.getState());

        //打印出blocked,因为thread2想要拿到synchronized却不得
        System.out.println(thread2.getState());

        Thread.sleep(1300);
        System.out.println(thread1.getState());


    }


    @Override
    public void run() {
        syc();
    }

    private synchronized void syc(){
        try {
            Thread.sleep(1000);
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
